NVM.Clipboard = function () {
    var __core = NOVEM.Core;

    return {
        init: function (selector) {
            if (__core.getSelector(selector, false, false).length < 1) {
                return true;
            }

            __core.loadJS({ file: 'plugins/clipboard.js', id: 'nvm-clipboard-js', jsFolder: true });

            __core.isFuncTrue(function () {
                return typeof ClipboardJS !== 'undefined';
            }).then(function (cond) {
                if (!cond) {
                    return false;
                }

                __core.initFunction({ class: 'has-plugin-clipboard', event: 'pluginClipboardReady' });

                selector = __core.getSelector(selector, false);
                if (selector.length < 1) {
                    return true;
                }

                var clipboards = [],
                    count = 0;

                // const btnTitle = '复制链接';
                // const btnShare = '复制转发语和链接';

                // const btnHtml = [
                //     '<div class="clipboard-copy">',
                //     '  <div class="clipboard">',
                //     '    <button type="button" class="btn btn-clipboard">',
                //     '      <svg class="bi" role="img" aria-label="复制"><use xlink:href="#clipboard"/></svg>',
                //     '    </button>',
                //     '  </div>',
                //     '</div>'
                // ].join('');

                // document.querySelectorAll('.nvm-clipboard a')
                //     .forEach(element => {
                //         if (!element.closest('.nvm-example')) {
                //             element.insertAdjacentHTML('beforebegin', btnHtml)
                //             element.previousElementSibling.append(element)
                //         }
                //     });

                // function nvmButtonTooltip(selector, title) {
                //     document.querySelectorAll(selector).forEach(btn => {
                //         bootstrap.Tooltip.getOrCreateInstance(btn, { title })
                //     })
                // }

                // nvmButtonTooltip('.btn-clipboard', btnTitle);
                // nvmButtonTooltip('.btn-share-clipboard', btnShare);

                // const clipboard = new ClipboardJS('.btn-clipboard', {
                //     target: trigger => trigger.closest('.clipboard-copy').querySelector('a'),
                //     text: trigger => trigger.closest('.clipboard-copy').querySelector('a').getAttribute('href')// textContent.trimEnd()
                // });

                // clipboard.on('success', event => {
                //     const iconFirstChild = event.trigger.querySelector('.bi').firstElementChild;
                //     const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger);
                //     const namespace = 'http://www.w3.org/1999/xlink';
                //     const originalXhref = iconFirstChild.getAttributeNS(namespace, 'href');
                //     const originalTitle = event.trigger.title;
                //     const isCheckIconVisible = originalXhref === '#check2';

                //     if (isCheckIconVisible) {
                //         return
                //     }

                //     tooltipBtn.setContent({ '.tooltip-inner': '已复制' });
                //     event.trigger.addEventListener('hidden.bs.tooltip', () => {
                //         tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
                //     }, { once: true });
                //     event.clearSelection();
                //     iconFirstChild.setAttributeNS(namespace, 'href', originalXhref.replace('clipboard', 'check2'));

                //     setTimeout(() => {
                //         iconFirstChild.setAttributeNS(namespace, 'href', originalXhref)
                //         event.trigger.title = originalTitle
                //     }, 2000);
                // });

                // clipboard.on('error', event => {
                //     const modifierKey = /mac/i.test(navigator.userAgent) ? '\u2318' : 'Ctrl-'
                //     const fallbackMsg = `Press ${modifierKey}C to copy`
                //     const tooltipBtn = bootstrap.Tooltip.getInstance(event.trigger)

                //     tooltipBtn.setContent({ '.tooltip-inner': fallbackMsg })
                //     event.trigger.addEventListener('hidden.bs.tooltip', () => {
                //         tooltipBtn.setContent({ '.tooltip-inner': btnTitle })
                //     }, { once: true })
                // });

                selector.forEach(function (el) {
                    var trigger = el.querySelector('button'),
                        triggerText = trigger.innerHTML,
                        copiedtext = trigger.getAttribute('data-copied') || '已复制',
                        copiedTimeout = trigger.getAttribute('data-copied-timeout') || 5000;

                    clipboards[count] = new ClipboardJS(trigger, {
                        target: function (content) {
                            return content.closest('.nvm-clipboard').querySelector('p');
                        }
                    });

                    clipboards[count].on('success', function (e) {
                        trigger.innerHTML = copiedtext;
                        trigger.disabled = true;

                        setTimeout(function () {
                            trigger.innerHTML = triggerText;
                            trigger.disabled = false;
                        }, Number(copiedTimeout));
                    });

                    count++;
                });
            });
        }
    };
}();
